<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

    <body>
        <ui:composition template="../templates/default3.xhtml">  
            <ui:define name="content">
                <h:form id ="purchaseItem" binding="#{purchaseOrderManagerBean.loadPurchaseItem}">
                    <p:panel header="Purchase Order Information">
                        <p:messages id="error"/>

                        <table class="panel">
                            <tr>
                                <td width ="180px">Company:</td>
                                <td width="200px"> <h:outputText id ="company" value="#{purchaseOrderManagerBean.customer.company}"  />  
                                </td>
                            </tr>
                            <tr>
                                <td>Company address:</td>
                                <td><h:outputText id ="address" value="#{purchaseOrderManagerBean.companyAddress.street}, 
                                                  #{purchaseOrderManagerBean.companyAddress.country}, #{purchaseOrderManagerBean.companyAddress.postal}             " /> </td>
                            </tr>
                            <tr>
                                <td>Purchase order no.:</td>
                                <td><h:outputText id="poNumber" value="#{purchaseOrderManagerBean.poNumber}" /></td>
                            </tr>
                            <tr>
                                <td>Purchase order date:</td>
                                <td>  
                                    <h:outputText id="poDate" value="#{purchaseOrderManagerBean.poDate}" >
                                        <f:convertDateTime pattern="dd-MM-yyyy" />
                                    </h:outputText>
                                </td>
                            </tr>
                        </table>  
                    </p:panel>
                    <p:panel header="Purchase item">
                        <table class="panel" width="100%">
                            <tr>
                                <td width="150">Product:</td>
                                <td>
                                    <h:selectOneMenu id="selectOneProduct" value="#{purchaseOrderManagerBean.product.code}">
                                        <f:selectItem itemLabel="Select Product" itemValue="" />
                                        <f:selectItems value="#{purchaseOrderManagerBean.products}" var="product" 
                                                       itemLabel="#{product.name}" itemValue="#{product.code}"/>
                                    </h:selectOneMenu>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="150">Unit Price ($):</td>
                                <td><p:inputText id="unitPrice" value="#{purchaseOrderManagerBean.unitPrice}" required="true"
                                                 requiredMessage="Unit Price Required" label="Unit Price">
                                        <f:convertNumber minFractionDigits="2" />                                      
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>

                            </tr>
                            <tr>
                                <td width="150">Quantity:</td>
                                <td><p:inputText id="quantity" value="#{purchaseOrderManagerBean.quantity}" required="true"
                                                 requiredMessage="Quantity Required" label="Quantity"> 
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="150">Discount (%):</td>
                                <td><p:inputText id="discount" value="#{purchaseOrderManagerBean.discount}" required="true"
                                                 requiredMessage="Discount Required" label="Discount">
                                        <f:convertNumber minFractionDigits="2" />                                 
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="150">Shipping location:</td>
                                <td>  <p:dataTable id="AddressS" var="address" value="#{purchaseOrderManagerBean.shippingAddresses}" paginator="false" 
                                                   selectionMode="single" selection="#{purchaseOrderManagerBean.shippingAddress}"
                                                   emptyMessage="No address found with given criteria." >                               

                                        <p:column headerText="Street"> 
                                            <h:outputText value="#{address.street}" />                                             
                                        </p:column> 
                                        <p:column headerText="Postal Code"> 
                                            <h:outputText value="#{address.postal}" />                                             
                                        </p:column> 
                                        <p:column headerText="City"> 
                                            <h:outputText value="#{address.city}" />                                             
                                        </p:column> 
                                        <p:column headerText="Country"> 
                                            <h:outputText value="#{address.country}" />                                             
                                        </p:column> 
                                    </p:dataTable>
                                </td>
                            </tr>

                            <tr>
                                <td width="150">Shipping rate (case):</td>
                                <td><p:inputText id="srate" value="#{purchaseOrderManagerBean.shippingRate}"  label="Shipping Rate">
                                        <f:convertNumber minFractionDigits="2" />                                 
                                    </p:inputText> 
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td>Request Delivery Date:</td>
                                <td>
                                    <p:calendar requiredMessage="Request Delivery Date Required" 
                                                value="#{purchaseOrderManagerBean.requestDate}"  />  
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" class="btn">
                                    <hr/>
                                    <p:commandButton value="Add PO Item" ajax="true"
                                                     actionListener="#{purchaseOrderManagerBean.addNewPurchaseItem}" update="purchaseItem"/>         </td>
                            </tr>
                        </table>
                    </p:panel>
                    <p:panel header="Purchase Items">
                        <table width="100%">
                            <tr>
                                <td align="left">
                                    <h:outputText id="output" value="#{purchaseOrderManagerBean.poNumber} 
                                                  has #{purchaseOrderManagerBean.itemsCount} order items." />
                                </td>
                            </tr>
                        </table>
                        <p:dataTable id="purchaseItem" var="purchaseItem" value="#{purchaseOrderManagerBean.purchaseItems}" paginator="false"                                     
                                     selectionMode ="single" selection="#{purchaseOrderManagerBean.selectedPurchaseItem}" emptyMessage="No item found with given criteria.">                                                             

                            <p:column headerText="Product name"> 
                                <h:outputText value="#{purchaseItem.product.name}" />                                             
                            </p:column> 
                            <p:column headerText="Quantity"> 
                                <h:outputText value="#{purchaseItem.quantity}" />                                             
                            </p:column> 
                            <p:column headerText="Unit price"> 
                                <h:outputText value="#{purchaseItem.unitPrice}">
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column>      
                            <p:column headerText="Shipping rate"> 
                                <h:outputText value="#{purchaseItem.shippingRate}">
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column>      
                            <p:column headerText="Discount"> 
                                <h:outputText value="#{purchaseItem.discount}" />                                             
                            </p:column> 
                            <p:column headerText="Sub Total"> 
                                <h:outputText value="#{purchaseItem.subTotal}">
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column>     
                            <p:column headerText="Shipping"> 
                                <h:outputText value="#{purchaseItem.shippingAddress.street}, #{purchaseItem.shippingAddress.city}" />                                             
                            </p:column>     
                        </p:dataTable>
                        <p:commandButton value="Remove item" actionListener="#{purchaseOrderManagerBean.remove}" update="purchaseItem" ajax="true"/>
                    </p:panel>
                    <table class="panel">
                        <tr>
                            <td class="btn">
                                <p:commandButton value="Done" actionListener="#{purchaseOrderManagerBean.done}" update="error" ajax="true" />
                            </td>
                        </tr>
                    </table>
                </h:form>
            </ui:define>
        </ui:composition>
    </body>
</html>
